<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.branchid" placeholder="网点编号" clearable></el-input>
      </el-form-item>
      <el-form-item>  
        <el-input v-model="dataForm.branchname" placeholder="网点名称" clearable></el-input>
       </el-form-item>  
       <el-form-item> 
        <el-select v-model="dataForm.attechorg" clearable placeholder="归属参与者" v-if="!userorgid">
            <el-option
              v-for="item in bankList"
              :key="item.bankid"
              :label="item.bankname"
              :value="item.bankid">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary"  @click="getDataList()">本地查询</el-button>
        <el-button type="primary" @click="export2Excel('table')">导出</el-button>
        <el-button v-if="isAuth('sys:bdcbranchinfo:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('sys:bdcbranchinfo:save')" type="primary" @click="checkNeedDoubleCheck()">批量导入</el-button>
        <el-button v-if="isAuth('sys:bdcbranchinfo:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
<el-table
      :data="dataList"
      border
      id="table"
      @selection-change="selectionChangeHandle"
      style="width: 100%;display:none;">

      <el-table-column
        prop="branchid"
        header-align="center"
        align="center"
        width="130"
        label="网点编号">
      </el-table-column>
      <el-table-column
        prop="branchname"
        header-align="center"
        align="center"
            :show-overflow-tooltip="true"
        label="网点名称">
      </el-table-column>

      <el-table-column
        prop="attechorg"
        header-align="center"
        align="center"
         width="130"
        label="归属参与者">
      </el-table-column>
      <el-table-column
        prop="region"
        header-align="center"
        align="center"
        width="100"
        label="所属区域">
      </el-table-column>

      <el-table-column
        prop="state"
        header-align="center"
        align="center"
        width="100"
        label="机构状态">
      </el-table-column>
      <el-table-column
        prop="enabledate"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="启用日期">
      </el-table-column>
      <el-table-column
        prop="disabledate"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="停用日期">
      </el-table-column>
      <el-table-column
        prop="lastupdate"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="更新日期">
      </el-table-column>

    </el-table>

    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      element-loading-text="加载中"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>

      <el-table-column
        prop="branchid"
        header-align="center"
        align="center"
        width="130"
        label="网点编号">
      </el-table-column>
      <el-table-column
        prop="branchname"
        header-align="center"
        align="center"
            :show-overflow-tooltip="true"
        label="网点名称">
      </el-table-column>

      <el-table-column
        prop="attechorg"
        header-align="center"
        align="center"
         width="130"
        label="归属参与者">
      </el-table-column>
      <el-table-column
        prop="region"
        header-align="center"
        align="center"
        width="100"
        label="所属区域">
      </el-table-column>

      <el-table-column
        prop="state"
        header-align="center"
        align="center"
        width="100"
        label="机构状态">
      </el-table-column>
      <el-table-column
        prop="enabledate"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="启用日期">
      </el-table-column>
      <el-table-column
        prop="disabledate"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="停用日期">
      </el-table-column>
      <el-table-column
        prop="lastupdate"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="更新日期">
      </el-table-column>

      <el-table-column
        fixed="left"
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.seqno)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.seqno)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="datepicker.pageOptions"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <import-branch v-if="importBranchVisible" ref="importBranch" @refreshDataList="getDataList"></import-branch>
    <double-check v-if="doubleCheckVisible" ref="doubleCheck"  @refreshDataList="doubleCheckHandle" ></double-check>     
  </div>
</template>

<script>
  import AddOrUpdate from './bdcbranchinfo-add-or-update'
  import ImportBranch from './bdcbranchinfo-import' 
  import DoubleCheck from './double-check' 
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        bankList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        importBranchVisible: false,
        doubleCheckVisible: false,
        needDoubleCheck: false
      }
    },
    components: {
      AddOrUpdate,
      ImportBranch,
      DoubleCheck
    },
    computed: {
      userorgid: {
        get () { return this.$store.state.user.orgid }
      }
    },
    activated () {
      this.getDataList()
      this.init()
    },
    methods: {
      goback(){

      },
      export2Excel(id){
          this.datepicker.export2Excel(id, this, '网点信息.xlsx')
      },
      init(){
        this.$http({
          url: this.$http.adornUrl('/sys/bdcbranchinfo/item/list'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.bankList = data.bankList
            this.needDoubleCheck = data.needDoubleCheck
          } 
        })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/bdcbranchinfo/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'branchid': this.dataForm.branchid,
            'branchname': this.dataForm.branchname,
            'attechorg': this.dataForm.attechorg
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },

     checkNeedDoubleCheck(){
        if (this.needDoubleCheck){
          this.doubleCheckVisible = true;
          this.$nextTick(() => {
            this.$refs.doubleCheck.init()
          })                   
        }else{
          this.importHandle ();
        }
      },

      doubleCheckHandle(username, passwd, callback){

        this.$http({
            url: this.$http.adornUrl('/sys/bdcbranchinfo/doubleCheck'),
            method: 'post',
            data: this.$http.adornData({
              'username':username,
              'passwd':passwd
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  if (callback){
                    callback()
                  }
                  this.importHandle()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })

      },

      importHandle (){
        this.importBranchVisible = true
        this.$nextTick(() => {
          this.$refs.importBranch.init()
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.seqno
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/bdcbranchinfo/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
<style lang="scss">
body .el-table th.gutter{
    display: table-cell!important;

}
</style>